<?php

namespace SinoPHP\BbdBoss\Model;

use Upfor\UpforPHP\Model;

class ServerGroup extends Model {
    /**
     * 检查字段是否已经存在
     */
    public function getServerGroup($server_code = '',$id = '') {
        $sql = "SELECT * FROM `server_group` WHERE `server_code` = $server_code AND `server_group_id` != {$id}";

        $data = $this->db->query($sql);

        return $data;
    }
    /**
     * 获取服务器组列表
     *
     * @param string $fields
     * @param null   $limit
     * @param null   $offset
     * @return array
     */
    public function listRole($fields = '',$where = array(), $limit = null, $offset = null, $orderBy = []) {

        $sql = "SELECT `{$fields}` FROM (`server_group`) LEFT JOIN `server_group_role` ON `server_group`.`server_role_id` = `server_group_role`.`server_role_id`";

        if($where['query']){
            $sql .= " WHERE `server_name` LIKE '%".$where['query']."%' ";
        }

        $order = [];
        foreach ($orderBy as $field => $type) {
            switch ($field) {
                case 'type':
                    $order[] = '`server_group_role`.`type` ' . $type;
                    break;
                case 'role':
                    $order[] = '`server_group`.`server_role_id` ' . $type;
                    break;
                default:
                    $order[] = '`' . $field . '` ' . $type;
            }
        }
        if ($order) {
            $sql .= ' ORDER BY ' . implode(', ', $order);
        }

        if ($limit) {
            $sql .= " LIMIT {$offset},{$limit}";
        }

        $data = $this->db->query($sql);

        return $data;
    }

    /**
     * 查询服务器组
     * @param string $server_name
     * @param int   $id
     */
    public function getServerGroupName($server_name = '', $id = null) {

        $sql = "SELECT * FROM `server_group` WHERE `server_name` = '{$server_name}' AND `server_group_id` != {$id}";

        $data = $this->db->query($sql);

        return $data;
    }

    /**
     * @param int  $id
     * @param array $where
     * @param int  $limit
     * @param int  $offset
     * @return mixed
     */
    public function getIpBindList($id = null, $where = array(),$limit = null, $offset = null) {

        $sql = "SELECT * FROM `server_ip` WHERE `ip_id` NOT IN (SELECT `ip_id` FROM `server_group_ip` WHERE `server_group_id` = {$id} ) AND `belong_type` = 0 
            AND is_delete = 0 AND status = 1";

        if($where['search_type'] && ($where['query'] || 0 == $where['query'])){

            $sql .= " AND {$where['search_type']} LIKE '%".$where['query']."%' ";

        }
        if ($limit) {
            $sql .= "LIMIT {$offset},{$limit}";
        }
        $data = $this->db->query($sql);

        return $data;
    }

    /**
     * @param int  $id
     * @param array $where
     * @param int  $limit
     * @param int  $offset
     * @return mixed
     */
    public function getReleaseList($id = null,$where = array(), $limit = null, $offset = null) {

        $sql = "SELECT * FROM `server_ip` WHERE `ip_id` IN (SELECT `ip_id` FROM `server_group_ip` WHERE `server_group_id` = {$id})  AND `belong_type` = 0 AND is_delete = 0 AND status = 1";

        if($where['search_type'] && ($where['query'] || 0 == $where['query']) ){

            $sql .= " AND {$where['search_type']} LIKE '%".$where['query']."%' ";

        }
        if ($limit) {
            $sql .= "LIMIT {$offset},{$limit}";
        }
        $data = $this->db->query($sql);

        return $data;
    }
    public function getIpBindCount($id = null, $where = array()) {

        $sql = "SELECT COUNT(*) AS `count` FROM `server_ip` WHERE `ip_id` NOT IN (SELECT `ip_id` FROM `server_group_ip` WHERE `server_group_id` = {$id})";

        if($where['search_type'] && ($where['query'] || 0 == $where['query'])){

            $sql .= " AND {$where['search_type']} LIKE '%".$where['query']."%' ";

        }

        $data = $this->db->query($sql);

        return $data;
    }
    public function getReleaseCount($id = null, $where = array()) {

        $sql = "SELECT COUNT(*) AS `count` FROM `server_ip` WHERE `ip_id` IN (SELECT `ip_id` FROM `server_group_ip` WHERE `server_group_id` = {$id})";

        if($where['search_type'] && ($where['query'] || 0 == $where['query'])){

            $sql .= " AND {$where['search_type']} LIKE '%".$where['query']."%' ";

        }
        $data = $this->db->query($sql);

        return $data;
    }

    /**
     * 获取服务器组信息
     * 
     * @param  array $where
     * @return array
     */
    public function getGroupInfo($where) {
        $cols = [
            'server_group_id',
            'server_name',
            'server_code',
            'server_role_id'
        ];
        return $this->getRow($cols, $where);
    }
}
